<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Admin extends CI_Controller {

    public function Admin() {
        parent::__construct();
        $this->load->model('user_model');
        $this->load->model('admin_model');
        $this->load->model('summary_model');
        $this->load->library('administrator_authenticate');
    }

    private function _getTotalOfficialOrder() {
        $this->administrator_authenticate->check_login();
        $year = date("Y");
        $query = $this->db->query("SELECT ID FROM Official_Order WHERE YEAR(date) = '$year'");
        return $query->num_rows();
    }

    private function _getTotalMedalOrder() {
        $this->administrator_authenticate->check_login();
        $year = date("Y");
        $query = $this->db->query("SELECT SUM(Official_Medal_Order.possible_amount) total_amount FROM Official_Medal_Order INNER JOIN Official_Order ON Official_Medal_Order.Official_Order_ID = Official_Order.ID WHERE YEAR(Official_Order.date) = '$year'");
        $row = $query->result_array();
        return $row[0]['total_amount'];
    }

    private function _getTotalMedalReceipt() {
        $this->administrator_authenticate->check_login();
        $year = date("Y");
        $query = $this->db->query("SELECT ID FROM Medal_Reception WHERE YEAR(Medal_Reception.date) = '$year'");
        return $query->num_rows();
    }

    private function _getTotalMedalInStock() {
        $this->administrator_authenticate->check_login();
        $query = $this->db->query("SELECT SUM(amount) AS total_amount FROM Medal_Stock");
        $row = $query->result_array();
        return $row[0]['total_amount'];
    }

    private function _getTotalStockIssue() {
        $this->administrator_authenticate->check_login();
        $year = date("Y");
        $query = $this->db->query("SELECT ID FROM Medal_Stock_Issue WHERE YEAR(date) = '$year'");
        return $query->num_rows();
    }

    public function index() {
        $this->administrator_authenticate->check_login();
        /* $this->load->view('admin_summary_view'); */
        
        // summary data
        $data['content'] = 'admin_summary_view';
        $data['header'] = 'ស្ថិតិទិន្ន័យសង្ខេប';
        $data['jquery_ui'] = '';
        $data['jquery_dialog'] = '';
        $data['officialOrderAmount'] = $this->_getTotalOfficialOrder();
        $data['medalOrderAmount'] = $this->_getTotalMedalOrder();
        $data['medalReceiptAmount'] = $this->_getTotalMedalReceipt();
        $data['medalStockInAmount'] = $this->_getTotalMedalInStock();
        $data['medalStockIssueAmount'] = $this->_getTotalStockIssue();

        // for list display
        $data['newOfficialOrders'] = $this->db->query("
            SELECT Official_Order.ID, Official_Order.Order_Number, Official_Order.date, Official_status.name FROM Official_Order
            INNER JOIN Official_status ON Official_Order.order_status = Official_status.ID
            ORDER BY Official_Order.date DESC LIMIT 5
            ") -> result_array();
        $data['newMedalIssues'] = $this->db->query("
            SELECT issue_number, date FROM Medal_Stock_Issue ORDER BY date DESC LIMIT 5
            ") ->result_array();
        $data['officialFollowups'] = $this->db->query("
            SELECT ID, date, Order_Number FROM Official_Order WHERE order_status = 2  ORDER BY date LIMIT 5
            ") ->result_array();
        $this->load->view('admin_template', $data);
    }

    function admin_report() {
        $this->administrator_authenticate->check_login();
        $data['content'] = 'admin_report';
        $data['header'] = 'របាយការណ៍';
        $data['jquery_ui'] = '';
        $data['jquery_dialog'] = '';
        $this->load->view('admin_template', $data);
    }

    function stock_issue() {
        $this->administrator_authenticate->check_login();
        $where = "";
        if ($_POST['report-fromdate'] != '' && strpos($_POST['report-fromdate'], '/')) {
            $where .= " AND Medal_Stock_Issue.date >= '" . $this->db->convertDate(check_post('report-fromdate')) . "'";
        }
        if ($_POST['report-todate'] != '' && strpos($_POST['report-todate'], '/')) {
            $where .= " AND Medal_Stock_Issue.date <= '" . $this->db->convertDate(check_post('report-todate')) . "'";
        }
        $query = $this->db->query("
                    SELECT SUM(Medal_Stock_Issue_Amount.amount) AS amount, Medal.Medal_Class_ID, Medal.Medal_Type_ID
                    FROM Medal_Stock_Issue
                    INNER JOIN Medal_Stock_Issue_Amount ON Medal_Stock_Issue_Amount.Medal_Stock_Issue_ID = Medal_Stock_Issue.ID $where
                    RIGHT JOIN Medal On Medal.ID = Medal_Stock_Issue_Amount.Medal_ID
                    GROUP BY Medal.Medal_Class_ID, Medal.Medal_Type_ID
        ");
        $result = $query->result_array();
        echo json_encode($result);
    }

    function get_issue_by_date(){
        $this->administrator_authenticate->check_login();
        $where = "";
        if ($_POST['report-fromdate'] != '' && strpos($_POST['report-fromdate'], '/')) {
            $where .= " Medal_Stock_Issue.date >= '" . $this->db->convertDate(check_post('report-fromdate')) . "'";
        }
        if ($_POST['report-todate'] != '' && strpos($_POST['report-todate'], '/')) {
            $where .= $where != "" ? " AND " : $where ;
            $where .= " Medal_Stock_Issue.date <= '" . $this->db->convertDate(check_post('report-todate')) . "'";
        }
        if(trim($where) != ''){
            $where ="WHERE $where";
        }
        $query = $this->db->query("
                    SELECT * FROM Medal_Stock_Issue $where 
       ");
        $result = $query->result_array();
        echo json_encode($result);
        
    }
    
    public function newofficialorder() {
        $this->administrator_authenticate->check_login();
        $this->load->model('official_order_model');
        $output = $this->official_order_model->getNewOfficial();
        echo json_encode($output);
    }

    public function newissue() {
        $this->administrator_authenticate->check_login();
        $this->load->model('medal_stock_issue_model');
        $output = $this->medal_stock_issue_model->getNewIssue();
        echo json_encode($output);
    }

    public function officialfollow() {
        $this->administrator_authenticate->check_login();
        $this->load->model('official_order_model');
        $output = $this->official_order_model->getOfficialFollow();
        echo json_encode($output);
    }

}